﻿using DBMode;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModels
{
    public class CommodityViewModel:BindableBase
    {

        private readonly IMethods<Commodity> commodityMethods;
        private readonly IDialogService dialogService;
        private readonly IMethods<Record> recordMethods;

        public CommodityViewModel(IMethods<Commodity> CommodityMethods, IDialogService dialogService,IMethods<Record> recordMethods)
        {
            commodityMethods = CommodityMethods;
            this.dialogService = dialogService;
            this.recordMethods = recordMethods;
            Refresh();
        }
        private List<Commodity> commoditys=new List<Commodity>();
      
        public List<Commodity> Commoditys
        {
			get { return commoditys; }
			set { commoditys = value; RaisePropertyChanged(); }
		}

        public DelegateCommand AddComCommand { get { return new DelegateCommand(AddCom); } }
        public DelegateCommand<object> DelCommand { get { return new DelegateCommand<object>(Del); } }
        public DelegateCommand<object> EditCommand { get { return new DelegateCommand<object>(Edit); } }

       

        /// <summary>
        /// 打开商品明细表
        /// </summary>
        private void AddCom()
        {
            IDialogParameters parameters = new DialogParameters();
            parameters.Add("add",null);
            dialogService.ShowDialog("AddCommodityView", parameters, callback =>
            {
                if (callback.Result == ButtonResult.OK)
                { Refresh(); }
            });
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Edit(object obj)
        {
            if (obj is Commodity commodity)
            {
                int a = commodityMethods.Update(commodity);
                if (a != 0) MessageBox.Show("操作成功");
                else
                    MessageBox.Show("操作失败");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Del(object obj)
        {
            if (obj is Commodity commodity)
            {
                int a = commodityMethods.Delete(commodity);
                if (a != 0) { 
                MessageBox.Show("操作成功");
                    Refresh();
                } 
                else
                    MessageBox.Show("操作失败");
            }
        }

        private void Refresh()
        {
            Commoditys = commodityMethods.Query();
            Commoditys.ForEach(c => {
                int intput = recordMethods.Query().FindAll(i => i.ComName == c.Name && i.Type == true).Sum(i=>i.Number);
                int output = recordMethods.Query().FindAll(i => i.ComName == c.Name && i.Type == false).Sum(i=>i.Number);
                c.Sum = intput - output;
                c.Total = (float)((c.Sum) * c.Price);
            });
        }
    }
}
